home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / Tumbler and Podium / Tumbler_teutilities.c < prev    next >
Encoding:
Text File  |  1995-11-13  |  4.2 KB  |  171 lines  |  [TEXT/MPS ]

  1. //
  2. //
  3. //        teutilities.c
  4. //
  5. //        Supplemental TextEdit utilities.
  6. //        
  7. //
  8. //        Author:        Nick Thompson & Pablo Fernicola, with thanks to the QuickDraw 3D team
  9. //        Date:        Monday, February 3, 1992
  10. //
  11. //        Copyright © 1992-95 Apple Computer, Inc., All Rights Reserved
  12. //
  13. //
  14.  
  15. #include <Script.h>
  16. #include "Tumbler_prototypes.h"
  17.  
  18. #include "Tumbler_teutilities.h"
  19.  
  20. //
  21. //    TEICut is an intelligent version of TECut. If the selection is a range
  22. //    of words, TEICut cuts the selection and removes a space from either the
  23. //    beginning or end of the selection point when the result of cutting leaves
  24. //    one too many spaces in a sentence. The resulting change in size of the
  25. //    TextEdit record is returned.
  26. //
  27. //    NOTE: This function does not work properly in all situations. It may still
  28. //        need some work.
  29. //
  30.  
  31. short TEICut(TEHandle theTE)
  32.  
  33. {    OffsetTable        startOffsets, endOffsets;
  34.     short            selStart, selEnd, characters;
  35.     Handle            theScrap;
  36.  
  37.     if (!(characters = (**theTE).selStart - (**theTE).selEnd))
  38.         return(0);
  39.  
  40.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selStart, true,
  41.                     0L, startOffsets);
  42.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selEnd, false,
  43.                     0L, endOffsets);
  44.  
  45.     if ((startOffsets[0].offFirst == (**theTE).selStart) &&
  46.         (endOffsets[0].offSecond  == (**theTE).selEnd)) {
  47.  
  48.         //
  49.         //    Both the beginning and end of the current selection is on
  50.         //    word boundaries.
  51.         
  52.  
  53.         selStart = (**theTE).selStart;
  54.         selEnd = (**theTE).selEnd;
  55.         if ((*((**theTE).hText))[selStart - 1] == ' ') {
  56.             TESetSelect(selStart - 1, selEnd, theTE);
  57.             TECut(theTE);
  58.             theScrap = TEScrapHandle();
  59.             BlockMove((char *) (*theScrap) + 1, (*theScrap), TEGetScrapLength() - 1);
  60.             TESetScrapLength(TEGetScrapLength() - 1);
  61.             ZeroScrap();
  62.             TEToScrap();
  63.             characters--;
  64.         } else if ((*((**theTE).hText))[selEnd] == ' ') {
  65.             TESetSelect(selStart, selEnd + 1, theTE);
  66.             TECut(theTE);
  67.             TESetScrapLength(TEGetScrapLength() - 1);
  68.             ZeroScrap();
  69.             TEToScrap();
  70.             characters--;
  71.         } else {
  72.             TECut(theTE);
  73.         }
  74.     } else {
  75.         TECut(theTE);
  76.     }
  77.     return(characters);
  78. }
  79.  
  80.  
  81. //
  82. //    TEIPaste is an intelligent version of TEPaste. When pasting into a sentence,
  83. //    an extra space may be added to either the start or end of the insertion
  84. //    to maintain readibility of the resulting sentence. Optionally, pointers
  85. //    may be provided to spaceBefore and/or spaceAfter. These flags return
  86. //    true if a space was inserted before or after the insertion respectively.
  87. //
  88. //    NOTE: This function does not always work properly. It still needs work.
  89. //
  90.  
  91. short TEIPaste(TEHandle theTE, short *spaceBefore, short *spaceAfter)
  92.  
  93. {    OffsetTable        startOffsets, endOffsets;
  94.     short            addSpaceAfter, characters;
  95.  
  96.     characters = (**theTE).selStart - (**theTE).selEnd;
  97.  
  98.     if (spaceBefore)
  99.         *spaceBefore = false;
  100.     if (spaceAfter)
  101.         *spaceAfter  = false;
  102.  
  103.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selStart, false,
  104.                     0L, startOffsets);
  105.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selEnd, true,
  106.                     0L, endOffsets);
  107.  
  108.     addSpaceAfter = ((endOffsets[0].offFirst == (**theTE).selEnd) &&
  109.                      ((*((**theTE).hText))[(**theTE).selEnd] != ' '));
  110.  
  111.     if ((startOffsets[0].offSecond == (**theTE).selStart) &&
  112.             ((*((**theTE).hText))[(**theTE).selStart - 1] != ' ')) {
  113.         TEKey(' ', theTE);
  114.         characters++;
  115.         if (spaceBefore)
  116.             *spaceBefore = true;
  117.     }
  118.  
  119.     TEPaste(theTE);
  120.     characters += TEGetScrapLength();
  121.  
  122.     if (addSpaceAfter) {
  123.         TEKey(' ', theTE);
  124.         characters++;
  125.         if (spaceAfter)
  126.             *spaceAfter = true;
  127.     }
  128.     return(characters);
  129. }
  130.  
  131.  
  132. //
  133. //    TEIsFrontOfLine, given an offset and a TextEdit handle, returns true if
  134. //    the given offset is at the beginning of a line start.
  135. //
  136.  
  137. short TEIsFrontOfLine(short offset, TEHandle theTE)
  138.  
  139. {    short        line = 0;
  140.  
  141.     if ((**theTE).teLength == 0)
  142.         return(true);
  143.  
  144.     if (offset >= (**theTE).teLength)
  145.         return( (*((**theTE).hText))[(**theTE).teLength - 1] == 0x0d );
  146.  
  147.     while ((**theTE).lineStarts[line] < offset)
  148.         line++;
  149.  
  150.     return( (**theTE).lineStarts[line] == offset );
  151. }
  152.  
  153.  
  154. //
  155. //    TEGetLine, given an offset and a TextEdit handle, returns the line number
  156. //    of the line that contains the offset.
  157. //
  158.  
  159. short TEGetLine(short offset, TEHandle theTE)
  160.  
  161. {    short        line = 0;
  162.  
  163.     if (offset > (**theTE).teLength)
  164.         return((**theTE).nLines);
  165.  
  166.     while ((**theTE).lineStarts[line] < offset)
  167.         line++;
  168.     
  169.     return(line);
  170. }
  171.